package com.thrd.taiexhibition.ui;


import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

import androidx.annotation.NonNull;

import com.thrd.basicframework.utils.ScreenUtils;


/**
 * 一划一屏ScrollView
 */
public class ScreenScrollView extends ScrollView {
    private static final String TAG ="ScreenScrollView";

    private Handler mHandler=new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            int top=msg.arg1% mScreenHeight;
            if(msg.arg1>mContentheight){
                if(msg.what==1){
                    smoothScrollTo(0,heightSum);
                }else{
                    smoothScrollTo(0,mContentheight);
                }

            }else if(msg.what==1){ //下方页
                if(top>mLimitTop){
                    smoothScrollTo(0,msg.arg1+(1200-top));
                }else{
                    smoothScrollTo(0,msg.arg1-top);
                }
            }else if(msg.what==2){//下方页
                if(mLimitBottom>top){
                    smoothScrollTo(0,msg.arg1-top);
                }else{
                    smoothScrollTo(0,msg.arg1+(1200-top));
                }
            }

        }
    };
    private int mScreenHeight;
    private int mLimitTop;       //上滑界限
    private int mLimitBottom;       //下滑界限
    private  int mContentheight;   //内容高度

    private static final float SLIDE_PERCENTAGE= 0.15f; //滑动百分比
    private int heightSum;
    //  private VelocityTracker veloctiy;

    public ScreenScrollView(Context context) {
        super(context);
        initView();
    }

    public ScreenScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ScreenScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void  initView(){
        mScreenHeight = ScreenUtils.getScreenHeight(getContext());
        mLimitTop = (int) (mScreenHeight*SLIDE_PERCENTAGE);
        mLimitBottom=mScreenHeight-mLimitTop;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        //调用ViewGroup类中测量子类的方法
//        measureChildren(widthMeasureSpec, heightMeasureSpec);
//        //调用View类中默认的测量方法
//        Log.e("ScreenScrollView","widthMeasureSpec"+widthMeasureSpec+"heightMeasureSpec"+heightMeasureSpec);
//        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        //  int modeWidth=MeasureSpec.getMode(widthMeasureSpec);
//        int modeHeight=MeasureSpec.getMode(heightMeasureSpec);
//
//        int sizeWidth= MeasureSpec.getSize(widthMeasureSpec);
//        int sizeHeight= MeasureSpec.getSize(heightMeasureSpec);
//
//        int heightSum=0;
//        if(MeasureSpec.AT_MOST==modeHeight){
//            for(int i=0;i<getChildCount();i++){
//                View view=getChildAt(i);
//                view.measure(widthMeasureSpec,heightMeasureSpec);
//                MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
//                heightSum+=layoutParams.height+layoutParams.topMargin+layoutParams.bottomMargin;
//            }
//        }
//
//
//        sizeHeight=  modeHeight==MeasureSpec.EXACTLY?sizeHeight:heightSum;
//        Log.e("ScreenScrollView","sizeHeight"+sizeHeight);
//        setMeasuredDimension(sizeWidth,sizeHeight);


        heightSum = 0;
        for(int i=0;i<getChildCount();i++){
            View view=getChildAt(i);
            view.measure(0, 0);
            heightSum += view.getMeasuredHeight(); // 计算子项View 的宽高
        }
        //     Log.e("ScreenScrollView","sizeHeight"+heightSum);
        mContentheight = (heightSum /1200-1)*1200;

        super.onMeasure(widthMeasureSpec,heightMeasureSpec);

    }

    private int touchY=0;
    private int turnPageReverse=1;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction()==MotionEvent.ACTION_UP){
            if(touchY-ev.getY()>0){  //下方页
                turnPageReverse=1;
            }else{          //上翻页
                turnPageReverse=2;
            }
            mHandler.post(runnable);
        }
        return super.onTouchEvent(ev);

    }

    private int top =0;
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        top=t;
    }

    private Runnable runnable=new Runnable() {
        @Override
        public void run() {
            Message message=mHandler.obtainMessage();
            message.arg1=top;
            message.what=turnPageReverse;
            mHandler.sendMessage(message);
        }
    };


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(ev.getAction()==MotionEvent.ACTION_DOWN){
            touchY= (int) ev.getY();
        }
        return super.onInterceptTouchEvent(ev);
    }
}
